#!/bin/bash
read -p "Please Input The Vhost Name:" vhost
if [ ! -n "$vhost" ]; then
    echo "Please Input The Vhost Name"
    exit 0
fi

read -p "Please Input The Vport:" vport
if [ ! -n "$vport" ]; then
    echo "Please Input The Vport"
    exit 0
else
    if grep '^[[:digit:]]*$' <<< "$vport";then
        echo '';
    else
        echo 'Error Port!'
        exit 0
    fi

    port_pid=`/usr/sbin/lsof -i :$vport|grep -v "PID" | awk '{print $2}'`
    if [ "$port_pid" != "" ];then
       echo "Port Already Used!"
       exit 0
    fi
fi

name=devops
if [ ! -d "$PWD/apache/$vhost-sites" ]; then
    mkdir $PWD/apache/$vhost-sites
    cp ./apache/conf/default.conf ./apache/$vhost-sites/default.conf
    #sed -i "" "s/html/html\/web/g" ./apache/$vhost-sites/default.conf
    sed -i "s/domain/${vhost}/g" ./apache/$vhost-sites/default.conf
    sed -i "s/\/access/\/${vhost}-access/g" ./apache/$vhost-sites/default.conf
    sed -i "s/\/error/\/${vhost}-error/g" ./apache/$vhost-sites/default.conf
fi
if [ ! -d "$PWD/apps/$vhost" ]; then
    mkdir $PWD/apps/$vhost
fi

## docker
docker run --restart=always --name $vhost-apache-php -p $vport:80 \
    -v "$PWD/apache/$vhost-sites":/etc/apache2/sites-enabled \
    -v "$PWD/apache/logs":/var/log/apache2 \
    -v "$PWD/apps/$vhost":/var/www/html \
    -v "$PWD/php":/usr/local/etc/php \
    --link $name-mysql:$name-mysql \
    --link $name-postgres:$name-postgres \
    --link $name-mongo:$name-mongo \
    --link $name-memcached:$name-memcached \
    --link $name-redis:$name-redis \
    --link $name-elk:$name-elk \
    --link $name-rabbitmq:$name-rabbitmq \
    -d apache-php:5.6

